const path = require("path");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
// gzip压缩
const CompressionWebpackPlugin = require("compression-webpack-plugin");

function resolve(dir) {
  return path.join(__dirname, dir);
}
console.log(process.env.VUE_APP_BASE_API)
const api = process.env.VUE_APP_BASE_API;
// console.log(process.env.NODE_ENV);

module.exports = {
  publicPath: "/admin",
  assetsDir: "./static",
  pages: {
    index: {
      entry: "src/main.js",
      template: "public/index.html",
      filename: "index.html",
      title: "Index Pages",
      chunks: ["chunk-vendors", "chunk-common", "index"]
    }
  },
  lintOnSave: process.env.NODE_ENV === "development" ? "warning" : false,
  productionSourceMap: false,
  chainWebpack: config => {
    // console.log(34, config)
    config.resolve.alias.set("@", resolve("src"));
  },
  devServer: {
    https: false,
    open: false,
    proxy: {
      "/api": {
        target: api,
		changeOrigin: true,
      }
    }
  },

  configureWebpack: config => {
    if (process.env.NODE_ENV === "production") {
      config.plugins.push(
        // 去除console
        new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              drop_debugger: true,
              dead_code: true,
              drop_console: true,
              pure_funcs: ["console.log"]
            },
            warnings: false
          },
          sourceMap: false,
          parallel: true
        })
      );

      const productionGzipExtensions = ["html", "js", "css"];
      config.plugins.push(
        new CompressionWebpackPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
          threshold: 10240, // 只有大小大于该值的资源会被处理 10240
          minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
          deleteOriginalAssets: false // 删除原文件
        })
      );
    }
  }
};
